package com.chenhy.springbootvuesmbms.controller;

import com.chenhy.springbootvuesmbms.pojo.Provider;
import com.chenhy.springbootvuesmbms.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    ProviderService providerService;

    @GetMapping("/providerList")
    public String toProviderList(Model model){
        List<Provider> providers = providerService.queryAllProvider();
        model.addAttribute("providerList",providers);
        return "provider/providerList";
    }

    @GetMapping("/fuzzyQueryProvider")
    public String providerFuzzyQuery(String condition, Model model){
        List<Provider> providers = providerService.userFuzzyQuery(condition);
        model.addAttribute("providerList",providers);
        return "provider/providerList.html::providerSpace";
    }

    @GetMapping("/providerDetails")
    public String userDetails(int id, Model model){
        Provider provider = providerService.queryProviderById(id);
        model.addAttribute("provider",provider);
        return "provider/providerDetails";
    }

    @PostMapping("/addProvider")
    public String addProvider(Provider provider, Model model){
        providerService.addProvider(provider);
        model.addAttribute("msg","添加成功，可继续添加");
        return "provider/addProvider";
    }

    @GetMapping("/addProvider")
    public String toAddProvider(){
        return "provider/addProvider";
    }

    @GetMapping("/updateProvider")
    public String toUpdateProvider(int id, Model model){
        Provider provider = providerService.queryProviderById(id);
        model.addAttribute("provider",provider);
        return "provider/updateProvider";
    }

    @PostMapping("/updateProvider")
    public String updateProvider(Provider provider, Model model){
        providerService.updateProvider(provider);
        model.addAttribute("msg", "修改成功");
        return "provider/updateProvider";
    }

    @PostMapping("/deleteProvider")
    public String deleteProvider(int id){
        providerService.deleteProvider(id);
        return "redirect:/provider/providerList";
    }

}
